package hs.bank.Web;

import hs.bank.Data.CardMapper;
import hs.bank.Entity.Card;
import hs.bank.Entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.reflect.Array;
import java.util.ArrayList;

@Controller
@RequestMapping("/home")
public class HomeController {
    private CardMapper cardMapper;

    @Autowired
    public void setCardMapper(CardMapper cardMapper) {
        this.cardMapper = cardMapper;
    }

    @GetMapping
    public String homePage(Model model) {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        model.addAttribute("user", user);

        // all cards info
        ArrayList<Card> cards = (ArrayList<Card>) cardMapper.getByUserId(user.getUserId());
        model.addAttribute("cards", cards);

        return "home";
    }
}
